<?php
    // +----------------------------------------------------------------------
    // | Created by PHPstorm: JRKAdmin框架 [ JRKAdmin ] 
    // +----------------------------------------------------------------------
    // | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
    // +----------------------------------------------------------------------
    // | SiteUrl: http://www.luckyhhy.cn
    // +----------------------------------------------------------------------
    // | Author: LuckyHhy <jackhhy520@qq.com>
    // +----------------------------------------------------------------------
    // | Date: 2020/3/6-17:08
    // +----------------------------------------------------------------------
    // | Description:  
    // +----------------------------------------------------------------------


    namespace app\wechat\service;


    use library\File;
    use think\Db;
    use WeChat\Contracts\MyCurlFile;

    /**
     * Class MediaService
     * @package app\wechat\service
     */
    class MediaService
    {

        /**
         * @param $id
         * @param array $where
         * @return array|\PDOStatement|string|\think\Model|null
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/6
         * @name: news
         * @describe: 通过图文ID读取图文信息
         */
        public static function news($id, $where = [])
        {
            $data = Db::name('WechatNews')->where(['id' => $id])->where($where)->find();
            list($data['articles'], $articleIds) = [[], explode(',', $data['article_id'])];
            $articles = Db::name('WechatNewsArticle')->whereIn('id', $articleIds)->select();
            foreach ($articleIds as $article_id) {
                foreach ($articles as $article) {
                    if (intval($article['id']) === intval($article_id)) {
                        array_push($data['articles'], $article);
                    }
                    unset($article['create_by'], $article['create_at']);
                }
            }

            return $data;
        }

        /**
         * @param $url
         * @param string $type
         * @param array $videoInfo
         * @return mixed
         * @throws \WeChat\Exceptions\InvalidResponseException
         * @throws \WeChat\Exceptions\LocalCacheException
         * @throws \think\Exception
         * @throws \think\exception\PDOException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/6
         * @name: upload
         * @describe:上传图片永久素材，返回素材media_id
         */
        public static function upload($url, $type = 'image', $videoInfo = [])
        {
            $where = ['md5' => md5($url), 'appid' => WechatService::getAppid()];
            if (($mediaId = Db::name('WechatMedia')->where($where)->value('media_id'))) {
                return $mediaId;
            }
            $result = WechatService::WeChatMedia()->addMaterial(self::getServerPath($url), $type, $videoInfo);
            dataSave(
                'WechatMedia',
                [
                    'local_url' => $url,
                    'md5'       => $where['md5'],
                    'appid'     => WechatService::getAppid(),
                    'type'      => $type,
                    'media_url' => isset($result['url']) ? $result['url'] : '',
                    'media_id'  => $result['media_id'],
                ],
                'type',
                $where
            );

            return $result['media_id'];
        }

        /**
         * @param $local
         * @return MyCurlFile
         * @throws \WeChat\Exceptions\LocalCacheException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/6
         * @name: getServerPath
         * @describe:文件位置处理
         */
        private static function getServerPath($local)
        {
            if (file_exists($local)) {
                return new MyCurlFile($local);
            } else {
                return new MyCurlFile(File::down($local)['file']);
            }
        }
    }